﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using EpicBall.ScreenSystem;
using Microsoft.Xna.Framework.Graphics;
using FarseerPhysics.Factories;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;

namespace EpicBall.Levels.Classes.Enemies
{
    class Projectile : Death
    {

        private bool _deadly;
        private bool _flipped;
        private float _speed;

        public Projectile(PhysicsGameScreen screen, StaticTurret st, bool deadly, bool flipped,  Texture2D texture, float speed)
            : base(screen, texture)
        {
            //Take both variables out of contrsuctor
            _flipped = flipped;
            _deadly = deadly;
            _speed = -speed;
        }

        public override void Draw()
        {
            //base.Draw();
            Vector2 displayUnits = ConvertUnits.ToDisplayUnits(_body.Position);
            SpriteEffects effects = SpriteEffects.None;
            if (!_flipped)
                effects = SpriteEffects.FlipHorizontally;

         _screen.GameStack.SpriteBatch.Draw(_texture, new Rectangle((int)(displayUnits.X - _width / 2), (int)(displayUnits.Y - _height / 2), _width, _height), null, Color.White, 0f, Vector2.Zero, effects, 0.5f);
        }

        public override void Update(GameTime gameTime)
        {
            if(!_flipped)
                _body.Position += ConvertUnits.ToSimUnits(_speed,0);
            else
                _body.Position -= ConvertUnits.ToSimUnits(_speed, 0);

            if (_body.Position.X < 0 - ConvertUnits.ToSimUnits(100) || _body.Position.X > ConvertUnits.ToSimUnits(_screen.Width + 100))
                _active = false;
                
        }


        public bool BulletCollision(Fixture f1, Fixture f2, Contact contact)
        {
            if (f2.Body == _screen.Player)
            {
                _screen.isDead = true;
            }
            
            //Should remove the bullet from the world when it collides with anything besides the player.
            //Doesn't work.
            else
            {
                _active = false;
            }

            return false;
        }


    }
}
